const express = require('express')
const router = express.Router()

//签到验证
router.post("/sign", function (req, res) {
    var param = req.body;
    //签到状态 0为旷课，1为正常 ，2为迟到
    var signStat = 0;
    let signStartTime = this.signStartTime;
    let beLateTime = this.beLateTime;
    let beMissTime = this.beMissTime;
    //获取标准日期;
    var myDate = new Date();
    console.log("myDate: " + myDate);
    var mySignDate = moment(new Date().getTime()).format('YYYY-MM-DD HH:mm:ss');
    console.log("mySignDate: " + mySignDate);
    //获取当前日期
    var myTime = myDate.toLocaleTimeString().split(":");
    var Newmytime = signStartTime.split(":");
    var OldTime = myTime;
    var duration = moment
        .duration(response.currentTime.diff(loginTime))
        .asSeconds();
    if (myTime[0] - Newmytime[0] > 0) {
        myTime[1] += ((myTime[0] - Newmytime[0]) * 60);
    }
    //大于10分钟迟到
    if (Newmytime[1] - myTime[1] > 10) {
        signStat = 1;
    }
    db.all("INSERT INTO tb_Sign (stuID, SignTime,SignStat) VALUES (?,?,?)", [param.stuID, mySignDate, signStat], function (err, data2) {
        //插入成功返回给用户；
        if (!err) {
            res.send({ msg: "success" });
        }
        else {
            res.send({ msg: "failed" });
        }
    })
})


//更改密码；由于已经是登陆态，可以直接更改；
app.post('/modifyPassword', function (req, res) {
    var param = req.body;
    if (param.stuID.length != 0) {
        db.all("update tb_stdInfo set pwd = ? where stuID=?", [param.pwd, param.stuID], function (err, data) {
            if (!err) {
                res.send({ msg: "OK" });
            }
            else {
                res.send({ msg: "failed" });
            }
        })
    }
    else {
        res.send({ msg: "failed" });
    }
});

module.exports = router